/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pos.ui.beans;

import com.pos.domain.Customer;
import com.pos.services.CustomerService;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author Dipendra
 */
@Named("customerBean")
@ConversationScoped
public class CustomerBean implements Serializable {

    @Inject
    private transient CustomerService customerService;
    private Customer customer;
    private boolean editMode;

    @PostConstruct
    public void reset() {
        customer = new Customer();
    }

    public Customer getCustomer() {
        return this.customer;
    }

    public void createNewCustomer() {
        this.customer = new Customer();
        setEditMode(true);
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public boolean isEditMode() {
        return this.editMode;
    }

    public void save() {
        customerService.save(this.customer);
        this.reset();
        setEditMode(false);
    }

    public void edit(Object id) {
        Customer customer = customerService.find(id);
        if (customer != null) {
            this.customer = customer;
        }
        this.setEditMode(true);
    }

    public List<Customer> getCustomers() {
        return customerService.findAll();
    }

    public Customer findCustomer(Object id) {
        return customerService.find(id);
    }
}
